package com.yakaten.login;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.CookieHandler;
import java.net.CookiePolicy;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.seasar.framework.log.Logger;
import org.seasar.framework.util.ResourceUtil;

import com.yakaten.exception.MakeShopException;

public class MakeShopLogin {

	static Logger logger = Logger.getLogger(MakeShopLogin.class);

	public boolean doLogin(String userName, String password)
			throws MakeShopException {
		boolean result = false;
		CorrectedCookieManager manager = new CorrectedCookieManager();
		manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
		CookieHandler.setDefault(manager);

		String LOGIN_URL = ResourceUtil.getProperties("yakaten.properties")
				.getProperty("makeshop.login.url");
		OutputStreamWriter out = null;
		BufferedReader bufferedReader = null;
		HttpURLConnection connection = null;
		try {
			URL loginUrl = new URL(LOGIN_URL);

			connection = (HttpURLConnection) loginUrl.openConnection();

			connection.setDoOutput(true);

			out = new OutputStreamWriter(connection.getOutputStream(), "euc-jp");

			out.write("id=" + userName + "&passwd=" + password
					+ "&submanager=ok&etclogin=ok&shop="+ResourceUtil.getProperties("yakaten.properties").getProperty(
							"makeshop.login.shopid"));

			out.flush();
			InputStream urlStream = connection.getInputStream();
			bufferedReader = new BufferedReader(new InputStreamReader(
					urlStream, "euc-jp"));
			String ss = null;
			StringBuffer total = new StringBuffer();
			while ((ss = bufferedReader.readLine()) != null) {
				total.append(ss);
			}

			if("1".equals(ResourceUtil.getProperties("yakaten.properties").getProperty(
					"makeshop.debug.on"))){
				IOUtils.write(total, new FileOutputStream("/var/www/vhosts/00_default/user/makeshoplogin.html"),"shift_jis");
			}

			if (logger.isDebugEnabled()) {
				logger.debug(total);
			}
			if (total.indexOf("管理者ログイン") > -1) {
				result = true;

			}
		} catch (MalformedURLException e) {
			throw new MakeShopException(e);
		} catch (IOException e) {
			throw new MakeShopException(e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}

		return result;
	}

	public static void main(String[] args) {
		MakeShopLogin login = new MakeShopLogin();
		try {
			logger.debug(login.doLogin("ryumei", "andybest123"));
		} catch (MakeShopException e) {
			logger.error(e);
			e.printStackTrace();
		}
	}
}
